package com.lapula.crazyface;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class FaceListAdapter extends BaseAdapter {
	private Context context;
	private List<FaceItem> faceItemList;
	
	public FaceListAdapter(Context context) {
		this.context = context;
		faceItemList = new ArrayList<FaceItem>();
	}
	
	public void clearAll() {
    	if (faceItemList != null) {
    		faceItemList.clear();
    		faceItemList = new ArrayList<FaceItem>();
    	}
    }
    
    public void addAll (List<FaceItem> faceItems) {
    	faceItemList.addAll(faceItems);
    }
	
    @Override
    public int getCount() {
        return faceItemList.size();
    }

	@Override
	public FaceItem getItem(int index) {
		return faceItemList.get(index);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		FaceItem faceItem = getItem(position);
		if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.face_item, null);
        }
		
		ImageView faceImageView = (ImageView) convertView.findViewById(R.id.iv_face);
		faceImageView.setImageResource(faceItem.getFaceResId());
		
		TextView faceNameTextView = (TextView) convertView.findViewById(R.id.tv_face_name);
		faceNameTextView.setText(faceItem.getFaceName());
        return convertView;
	}
}
